package jp.ac.segakuen;

import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

public class MailListTableUtil {
	public static String insertNew(String address,String enableFlag) {
		MailListTable mlt = new MailListTable(address,enableFlag);
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	try{
    		pm.makePersistentAll(mlt);
    	}finally{
    		pm.close();
    	}

		return mlt.getId().toString();
	}
	
	@SuppressWarnings("unchecked")
	public static void resetbyId(String address) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(MailListTable.class);
	    query.setFilter("address == listtext");
	    query.declareParameters("String listtext");
	    try {
	    	List<MailListTable> results = (List<MailListTable>)query.execute(address);
		    if(results.iterator().hasNext()){
		    	for(MailListTable mlt:results){
		    		pm.deletePersistent(mlt);
		    	}
		    }
	    } catch (JDOObjectNotFoundException e) {
		        // Object isn't there, so ignore it.
	    	return;
	  } finally {
		    	pm.close();
	  }
	}
	@SuppressWarnings("unchecked")
	public static void fullreset() {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(MailListTable.class);

	    try {
	    	List<SelectListTable> results = (List<SelectListTable>)query.execute();
		    if(results.iterator().hasNext()){
		    	for(SelectListTable slt:results){
		    		pm.deletePersistent(slt);
		    	}
		    }
	    } catch (JDOObjectNotFoundException e) {
		        // Object isn't there, so ignore it.
	    	return;
	  } finally {
		    	pm.close();
	  }
	}
	
	
	@SuppressWarnings("unchecked")
	public static String getMailAddressList(){
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    StringBuilder mailaddresslist = new StringBuilder();
	    
	    Query query = pm.newQuery(MailListTable.class);
	    query.setFilter("enableFlag == '送信'");
	    try {
	    	List<MailListTable> results = (List<MailListTable>)query.execute();
		    if(results.iterator().hasNext()){
		    	for(MailListTable slt:results){
		    		mailaddresslist.append(slt.getAddress());
		    		mailaddresslist.append(",");
		    	}
		    }

	    } catch (JDOObjectNotFoundException e) {
		        // Object isn't there, so ignore it.
	    	return "Error";
	  } finally {
		    	pm.close();
	  }
	  return mailaddresslist.toString();
	}
	
	@SuppressWarnings("unchecked")
	public static void enableOrableSendMail(String address){
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(MailListTable.class);
	    query.setFilter("address == listtext");
	    query.declareParameters("String listtext");

	    try {
	    	List<MailListTable> results = (List<MailListTable>)query.execute(address);

		    if(results.iterator().hasNext()){
		    	for(MailListTable mlt:results){
		    		if(mlt.getEnableFlag().equals("送信")){
		    			mlt.setEnableFlag("送信しない");
		    		}else{
		    			mlt.setEnableFlag("送信");
		    		}
		    	}
		    }
	    } catch (JDOObjectNotFoundException e) {
		        // Object isn't there, so ignore it.
	    	return;
	  } finally {
		    	pm.close();
	  }
	}
}
